package com.ruoyi.djData.utils;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.djData.domain.plat.DataDj;

import java.text.DecimalFormat;

import static java.lang.Math.*;

/**
 * {}
 *
 * @Description:
 */
public class GonglvxiangUtils {
    public static DecimalFormat df1 = new DecimalFormat("0.0");
    public static DecimalFormat df2 = new DecimalFormat("0.00");
    public static DecimalFormat df3 = new DecimalFormat("0.000");
    public static DecimalFormat df4 = new DecimalFormat("0.0000");
    public static DecimalFormat df5 = new DecimalFormat("0.00000");
    public static DecimalFormat df6 = new DecimalFormat("0.000000");


    //根号3取1.732计算
    public static DataDj calculateFunc(DataDj dataDj) {
        double szgl = 0, yggl = 0, wggl = 0, fzxs = 0, scgl = 0, ygglsh = 0, xl = 0, kzwggl = 0, edfzwggl = 0, zhglsh = 0, edzhglsh = 0, zhxl = 0, zhxhgl = 0, edzhxhgl = 0, edzhxl = 0, wgbcrl = 0, wgbcdrl = 0, dypc = 0;
        double pjdy = dataDj.getPjdy().isEmpty() ? 0f : getNormalFloat(dataDj.getPjdy());
        double pjdl = dataDj.getPjdl().isEmpty() ? 0f : getNormalFloat(dataDj.getPjdl());
        double pjxdy = dataDj.getPjxdy().isEmpty() ? 0f : getNormalFloat(dataDj.getPjxdy());
        double UA = getNormalFloat(dataDj.getUA());
        double UB = getNormalFloat(dataDj.getUB());
        double UC = getNormalFloat(dataDj.getUC());
        double IA = getNormalFloat(dataDj.getIA());
        double IB = getNormalFloat(dataDj.getIB());
        double IC = getNormalFloat(dataDj.getIC());
        double UAB = getNormalFloat(dataDj.getUAB());
        double UBC = getNormalFloat(dataDj.getUBC());
        double UCA = getNormalFloat(dataDj.getUCA());
        double glys = dataDj.getGlysh().isEmpty() ? 0f : getNormalFloat(dataDj.getGlysh());
        System.out.println(glys);
        double edxl = getNormalFloat(dataDj.getDjEdxl());
        double edgl = getNormalFloat(dataDj.getDjEdgl());
        double kzgl = getNormalFloat(dataDj.getDjKzgl());
        double eddy = getNormalFloat(dataDj.getDjEddy());
        double kzdl = getNormalFloat(dataDj.getDjKzdl());
        double eddl = getNormalFloat(dataDj.getDjEddl());
        double wgjjdl = getNormalFloat(dataDj.getDjWgjjdl());
        double mbglys = getNormalFloat(dataDj.getDjMbglysh());
        double dwpl = getNormalFloat(dataDj.getDwpl());
        //
        if (dataDj.getDjCshff().equals("单瓦")) {
            pjdy = pjdy == 0f ? Double.parseDouble(df2.format(Math.max(UA, Math.max(UB, UC)))) : pjdy;
            pjdl = pjdl == 0f ? Double.parseDouble(df2.format(Math.max(IA, Math.max(IB, IC)))) : pjdl;
            pjxdy = pjxdy == 0f ? Double.parseDouble(df2.format(Math.max(UAB, Math.max(UBC, UCA)))) : pjxdy;

//            szgl = Float.parseFloat(df2.format(pjdy * pjdl * 3));
            szgl = Float.parseFloat(df2.format(pjxdy * pjdl * 1.732d / 1000d));

            yggl = Float.parseFloat(df2.format(szgl * glys));
            wggl = Float.parseFloat(df2.format(Math.sqrt(Math.pow(szgl, 2) - Math.pow(yggl, 2))));
        } else if (dataDj.getDjCshff().equals("双瓦")) {
            pjdy = pjdy == 0f ? Double.parseDouble(df2.format((UA + UB + UC) / 3)) : pjdy;
            pjdl = pjdl == 0f ? Double.parseDouble(df2.format((IA + IC) / 2)) : pjdl;
            pjxdy = pjxdy == 0f ? Double.parseDouble(df2.format((UAB + UBC + UCA) / 3)) : pjxdy;

            //修改前      szgl = Float.parseFloat(df2.format((UA * IA + UC * IC) * 1.5));
            szgl = Float.parseFloat(df2.format(pjxdy * pjdl * 1.732d / 1000d));

            yggl = Float.parseFloat(df2.format(szgl * glys));
            wggl = Float.parseFloat(df2.format(Math.sqrt(Math.pow(szgl, 2) - Math.pow(yggl, 2))));
        } else if (dataDj.getDjCshff().equals("三瓦")) {
            pjdy = pjdy == 0f ? Double.parseDouble(df2.format((UA + UB + UC) / 3)) : pjdy;
            pjdl = pjdl == 0f ? Double.parseDouble(df2.format((IA + IB + IC) / 3)) : pjdl;
            pjxdy = pjxdy == 0f ? Double.parseDouble(df2.format((UAB + UBC + UCA) / 3)) : pjxdy;

//            szgl = Float.parseFloat(df2.format(UA * IA + UB * IB + UC * IC));
            szgl = Float.parseFloat(df2.format(pjxdy * pjdl * 1.732d / 1000d));

            yggl = Float.parseFloat(df2.format(szgl * glys));
            System.out.println("szgl=====" + Math.pow(szgl, 2));
            System.out.println("yggl=====" + Math.pow(yggl, 2));
            System.out.println("1111===" + Math.sqrt(Math.pow(szgl, 2) - Math.pow(yggl, 2)));
            wggl = Float.parseFloat(df2.format((Double)Math.sqrt(Math.pow(szgl, 2) - Math.pow(yggl, 2))));
        }
        //修改前
//        szgl = szgl / 1000;
//        yggl = yggl / 1000;
//        wggl = wggl / 1000;

//        szgl = Float.parseFloat(df2.format(szgl));
//        yggl = Float.parseFloat(df2.format(yggl));
//        wggl = Float.parseFloat(df2.format(wggl));
        //修改后全部注释掉，不添加任何代码
//        double tmpA = Double.parseDouble(df2.format((1 / (edxl / 100) - 1) * edgl - kzgl));
//        double tmpB = Double.parseDouble(df2.format(tmpA * (yggl - kzgl)));
//        double tmpC = Double.parseDouble(df2.format(pow(edgl, 2) / 4 + tmpB));
        double tmpA = (1 / (edxl / 100) - 1) * edgl - kzgl;
        double tmpB = tmpA * (yggl - kzgl);
        double tmpC = (pow(edgl, 2) / 4 + tmpB);

        //负载系数
        if (edxl == 0 || tmpA == 0 || tmpC < 0) {
            fzxs = Double.parseDouble(df3.format(0));
        } else {
            fzxs = Double.parseDouble(df3.format((sqrt(tmpC) - edgl / 2) / tmpA));
        }
        System.out.println("edxl: " + edxl);
        System.out.println("edgl: " + edgl);
        System.out.println("kzgl: " + kzgl);
        System.out.println("yggl: " + yggl);
        System.out.println("fzxs: " + fzxs);
        //输出功率
        scgl = Double.parseDouble(df2.format(fzxs * edgl));
        System.out.println("输出功率: " + scgl);
        System.out.println("负载系数: " + fzxs);
        System.out.println("额定功率: " + edgl);
        //有功损耗
        if (edxl == 0) {
            ygglsh = 0.0f;
        } else {
            ygglsh = Double.parseDouble(df2.format(kzgl + fzxs * fzxs * tmpA));
        }
        //效率(电机效率)
        if (yggl == 0) {
            xl = 0.000f;
        } else {

//            xl = Double.parseDouble(df3.format(scgl / yggl));
            xl = Double.parseDouble(df2.format(scgl / yggl * 100f));

        }
        //修改前
//        xl = xl * 100;
//        xl = Double.parseDouble(df2.format(xl));
        //修改后


        //  Q1空载无功功率
        // if ((3 * ArrData[5] * ArrData[5] * kzdl * kzdl / 1000000 - kzgl * kzgl) > 0)
        //{
        // kzwggl = Math.round(Math.sqrt((3 * ArrData[5] * ArrData[5] * kzdl * kzdl / 1000000 - kzgl * kzgl)), 2);

        //}
        if ((3 * eddy * eddy * kzdl * kzdl / 1000000f - kzgl * kzgl) > 0) {
            kzwggl = Double.parseDouble(df2.format(sqrt(3 * eddy * eddy * kzdl * kzdl / 1000000f - kzgl * kzgl)));
            System.out.println("kzwggl: " + kzwggl);
        } else {
            kzwggl = 0.0f;
        }
        // Q2额定负载无功功率
        if (edxl == 0) {
            //|| edglys == 0 || (1 - pow(edglys, 2)) < 0) {
            edfzwggl = 0.0f;
        } else {
            //edfzwggl = Math.round(edgl / (edxl / 100) * (Math.sqrt(1 - edglys * edglys) / edglys), 2);
            edfzwggl = Double.parseDouble(df6.format(edgl / (edxl / 100000f) / sqrt(3) / eddl / eddy));
            edfzwggl = Double.parseDouble(df6.format(acos(edfzwggl)));
            edfzwggl = Double.parseDouble(df2.format(edgl / (edxl / 100f) * (tan(edfzwggl))));
        }
        //综合功率损耗
        zhglsh = Double.parseDouble(df2.format(kzgl + fzxs * fzxs * tmpA + wgjjdl * (kzwggl + (fzxs * fzxs) * (edfzwggl - kzwggl))));
        //额定综合功率损耗
        edzhglsh = Double.parseDouble(df2.format((1 / (edxl / 100f) - 1) * edgl + edfzwggl * wgjjdl));

        //综合效率
        if ((zhglsh + scgl) == 0) {
            zhxl = 0.0f;
        } else {

//            zhxl = Double.parseDouble(df3.format(scgl / (zhglsh + scgl)));
            zhxl = Double.parseDouble(df2.format(scgl / (zhglsh + scgl) * 100f));

        }
        //修改前
//        zhxl = zhxl * 100;
//        zhxl = Double.parseDouble(df2.format(zhxl));
        //    修改后


        if (xl > 100 || xl < 0) {
            xl = 0.0f;
        }
        if (zhxl > 100 || zhxl < 0) {
            zhxl = 0.0f;
        }
//        if (Double.isNaN(zhxl) || Double.isInfinite(zhxl)) {
//            zhxl = 0;
//        }
        ////            综合消耗功率 =负载率*额定功率+综合功率损耗
        zhxhgl = Double.parseDouble(df2.format(fzxs * edgl + zhglsh));
        ////额定综合消耗功率=额定功率+（1/额定效率-1）*额定功率+额定负载时的无功功率*无功经济当量
        edzhxhgl = Double.parseDouble(df2.format(edgl + ((1 / (edxl / 100f)) - 1) * edgl + edfzwggl * wgjjdl));
        ////额定综合效率=（额定功率/(额定功率+（1/额定效率-1）*额定功率+额定负载时的无功功率*无功经济当量）*100%)
        edzhxl = Double.parseDouble(df2.format(edgl / edzhxhgl * 100f));
//        fzxs = fzxs * 100;


        //无功补偿
        //            无功补偿容量=视在功率*(SQRT(1/功率因数/功率因数-1)-SQRT(1/目标功率因数/目标功率因数-1))
        //无功补偿电容量=无功补偿容量*1000000000/2/PI/电网频率/额定电压/额定电压
        if (glys != 0 && mbglys != 0) {
//            wgbcrl = Math.round(szgl * (Math.sqrt(1 / glys / glys - 1) - Math.sqrt(1 / mbglys / mbglys - 1)));
            wgbcrl = Double.parseDouble(df2.format(szgl * (Math.sqrt(1 / glys / glys - 1) - Math.sqrt(1 / mbglys / mbglys - 1))));
        } else {//Math.round四舍五入
            wgbcrl = 0.0f;
        }
        if (dwpl != 0) {//电网频率
//            wgbcdrl = Math.round(wgbcrl * 1000000000 / 2 / Math.PI / dwpl / eddy / eddy);
            wgbcdrl = Double.parseDouble(df2.format(wgbcrl * 1000000000f / 2 / Math.PI / dwpl / eddy / eddy));
        } else {
            wgbcdrl = 0.00f;
        }
        dypc = Double.parseDouble(df2.format(Math.abs(pjxdy - eddy)));//电源电压与额定电压偏差

        dataDj.setPjdy(doubleToStr(pjdy, 2));
        dataDj.setPjdl(doubleToStr(pjdl, 2));
        dataDj.setPjxdy(doubleToStr(pjxdy, 2));
        dataDj.setGlysh(doubleToStr(glys, 4));
        dataDj.setShzgl(doubleToStr(szgl, 2));
        dataDj.setYggl(doubleToStr(yggl, 2));
        dataDj.setWggl(doubleToStr(wggl, 2));
        dataDj.setFzxsh(doubleToStr(fzxs, 3));
        dataDj.setShchgl(doubleToStr(scgl, 2));
        dataDj.setYgglsh(doubleToStr(ygglsh, 2));
        dataDj.setDjxl(doubleToStr(xl, 2));
        dataDj.setKzwggl(doubleToStr(kzwggl, 2));
        dataDj.setEdfzwggl(doubleToStr(edfzwggl, 6));
        dataDj.setZhglsh(doubleToStr(zhglsh, 2));
        dataDj.setEdzhglsh(doubleToStr(edzhglsh, 2));
        dataDj.setZhxl(doubleToStr(zhxl, 2));
        dataDj.setZhxhgl(doubleToStr(zhxhgl, 2));
        dataDj.setEdzhxhgl(doubleToStr(edzhxhgl, 2));
        dataDj.setEdzhxl(doubleToStr(edzhxl, 2));
        dataDj.setWgbchrl(doubleToStr(wgbcrl, 2));
        dataDj.setWgbchdrl(doubleToStr(wgbcdrl, 2));
        dataDj.setDypch(doubleToStr(dypc, 2));
        String strres = "";
        if (zhxl > edzhxl) {
            strres = "经济运行";
        } else if (zhxl > (edzhxl * 0.6)) {
            strres = "允许运行";
        } else {
            strres = "非经济运行";
        }
        dataDj.setDjyxzht(strres);

        double preyggl = getNormalFloat(dataDj.getYggl());
        double prewggl = getNormalFloat(dataDj.getWggl());
        double preygdn = getNormalFloat(dataDj.getYgdn());
        double prewgdn = getNormalFloat(dataDj.getWgdn());
        dataDj.setYgdn(doubleToStr(preygdn + (preyggl + preyggl) / 2f / 3600f,2));//有功电能
        dataDj.setWgdn(doubleToStr(prewgdn + (prewggl + prewggl) / 2f / 3600f,2));//无功电能
        return dataDj;
    }


    /**
     * 不保留
     *
     * @param doubleVel
     * @return
     */
    private static String doubleToStr(double doubleVel) {
        return doubleToStr(doubleVel, 0);
    }

    /**
     * @param doubleVel
     * @param scale     保留位数
     * @return
     */
    private static String doubleToStr(double doubleVel, int scale) {
        float scaleSize = 1;
        while (scale-- > 0) {
            scaleSize *= 10f;
        }
        return (Double.isNaN(doubleVel) || Double.isInfinite(doubleVel)) ? "" : scaleSize == 1 ? doubleVel + "" : Math.round(doubleVel * scaleSize) / scaleSize + "";
    }

    public static float getNormalFloat(String value) {
        return StringUtils.isEmpty(value) ? 0f : Float.parseFloat(value);
    }

    public static String getNormalText(String text) {
        return StringUtils.isEmpty(text) ? "--" : text;
    }
}
